package com.example.zhapp_java.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhapp_java.R;
import com.example.zhapp_java.models.Milestone;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class MilestoneAdapter extends RecyclerView.Adapter<MilestoneAdapter.ViewHolder> {
    private final List<Milestone> milestoneList;
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

    public MilestoneAdapter(List<Milestone> milestoneList) {
        this.milestoneList = milestoneList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_milestone, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Milestone milestone = milestoneList.get(position);
        holder.tvMilestoneId.setText(milestone.getMilestoneId());
        holder.tvDate.setText(dateFormat.format(milestone.getMilestoneDate()));
        holder.tvRemarks.setText(milestone.getMilestoneRemarks());
    }

    @Override
    public int getItemCount() {
        return milestoneList.size();
    }

    public void updateList(List<Milestone> newList) {
        milestoneList.clear();
        milestoneList.addAll(newList);
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvMilestoneId, tvDate, tvRemarks;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvMilestoneId = itemView.findViewById(R.id.tvMilestoneId);
            tvDate = itemView.findViewById(R.id.tvMilestoneDate);
            tvRemarks = itemView.findViewById(R.id.tvMilestoneRemarks);
        }
    }
}